# -*- coding: utf-8 -*-
# @Author  : 廖善武
# @Email   : liaosw18878@hundsum.com
# @Time    : 2018/9/4 9:33
# @File    : OrtPublic.py

from Lib.ClassSelenium import ClassSelenium
from selenium.webdriver.common.keys import Keys
import time,datetime
from Lib.Log import logger
from Lib.Common import setFail

def login(username, password):
    # 登入
    # url = 'http://10.20.19.248:8080/Artemis/mainframe/frame.html'
    url = 'http://10.20.19.161/Artemis/login/login.html'
    global driver
    driver = ClassSelenium(url, 'chrome')
    element = driver.getElementByAttribute('xpath://input[@name="username"]')
    element.send_keys(username)
    element = driver.getElementByAttribute('id:user_password')
    element.send_keys(password)
    element = driver.getElementByAttribute('id:login_submit')
    element.click()
    logger.info("打开测试网址：" + url)
    return driver

def getMenu(menuName, elementObj):
    # 定位到具体菜单
    element = driver.getElementByAttribute('xpath://*[@id="dijit_form_TextBox_0"]')
    element.clear()
    element.send_keys(menuName)
    time.sleep(1)
    element.send_keys(Keys.ENTER)
    time.sleep(2)
    logger.info('测试开始：定位到【%s】菜单' %menuName)
    element = driver.getElementByAttribute(elementObj)
    time.sleep(1)
    element.click()

def click(clickElement, msg):
    # 点击
    element = driver.getElementByAttribute(clickElement)
    element.click()
    logger.info('点击"%s"' %msg)

def close(menuName):
    # 关闭测试窗口
    driver.close()
    logger.info('关闭【%s】测试窗口 '% menuName)

def add(addElement):
    # 点击添加
    element = driver.getElementByAttribute(addElement)
    element.click()
    logger.info('测试【添加】功能')
    logger.info('点击“添加”')

def update(updateDataElement, updateElement):
    # 点击修改
    element = driver.getElementByAttribute(updateDataElement)
    element.click()
    element = driver.getElementByAttribute(updateElement)
    element.click()
    logger.info('测试【修改】功能')
    logger.info('点击“修改”')

def select(message, selectElement, searchElement):
    # 下拉框选择内容
    element = driver.getElementByAttribute(selectElement)
    element.click()
    element = driver.getElementByAttribute(searchElement)
    element.send_keys(message)
    element.send_keys(Keys.ENTER)
    logger.info('选择的信息为：%s' %message)

def match(msg, clickElement, controlElement):
    # 下拉框控件匹配方法
    element = driver.getElementByAttribute(clickElement)
    element.click()
    elementList = driver.getElementByAttribute(controlElement, getAll=True)
    for element in elementList:
        if element.getText() == msg:
            time.sleep(0.5)
            element.click()
            logger.info('选中的内容为:%s' %msg)
            break

def write(message, elementObj ,msg):
    element = driver.getElementByAttribute(elementObj)
    element.clear()
    element.send_keys(message)
    logger.info('输入[%s]：%s' % (msg, message))

def writemethod(message, elementObj ,msg):
    element = driver.getElementByAttribute(elementObj)
    element.clear()
    element.send_keys(message)
    time.sleep(3)
    element.send_keys(Keys.ENTER)
    logger.info('输入[%s]：%s' % (msg, message))

def save(saveElement):
    element = driver.getElementByAttribute(saveElement)
    element.click()
    time.sleep(2)
    logger.info('点击“保存”')

def check(expectMessage, checkElement, flag=1):
    checkElementObj = driver.getElementByAttribute(checkElement)
    if flag == 1:
        checkMessage = checkElementObj.getText()
    else:
        checkMessage = checkElementObj.getAttributeText()
    logger.info('进行【断言】')
    logger.info('期望信息为：%s' % expectMessage)
    logger.info('断言信息为：%s' % checkMessage)
    if expectMessage == str(checkMessage):
        msg = "断言成功"
        logger.info('断言结果为：%s' % msg)
    else:
        msg = "断言失败！"
        driver.getScreen()
        setFail("断言失败！")
        logger.error('断言结果为：%s' % msg)
        driver.close()
        exit()

def query(elementObj):
    # 点击查询
    element = driver.getElementByAttribute(elementObj)
    element.click()
    logger.info('点击“查询”')

def reset(clickElement):
    elementObj = driver.getElementByAttribute(clickElement)
    elementObj.click()
    logger.info('点击“重置”')

def refresh(refreshElement):
    # 点击刷新
    element = driver.getElementByAttribute(refreshElement)
    element.click()
    logger.info('点击“刷新”')

def getHandleText(textElement):
    elementObj = driver.getElementByAttribute(textElement)
    time.sleep(1)
    text = elementObj.getText()
    logger.info('操作列表信息:%s'% text)
    return text

def searchbox(message, elementObj):
    element = driver.getElementByAttribute(elementObj)
    element.clear()
    element.send_keys(message)
    element.send_keys(Keys.ENTER)
    logger.info('查询信息为：%s' % message)

def delete(deleteDataElement, deleteElement):
    # 点击数据
    element = driver.getElementByAttribute(deleteDataElement)
    element.click()
    # 删除元素
    element = driver.getElementByAttribute(deleteElement)
    element.click()
    logger.info('测试【删除】功能')
    logger.info('点击“删除”')

def confirm():
    # 弹出框确定
    driver.getAlert()

def rightClick(targetElement):
    elementObj = driver.getElementByAttribute(targetElement)
    driver.rightClick(elementObj)
    logger.info('右键单击')

def getCurrentDate(flag = None):
    year = str(int(datetime.datetime.now().strftime('%Y')))
    month = str(int(datetime.datetime.now().strftime('%m')))
    if flag == None:
        day = str(int(datetime.datetime.now().strftime('%d')))
        currentDate = year + '-' + month + '-' + day
    else:
        day = str(int(datetime.datetime.now().strftime('%d')) - flag)
        currentDate = year + '-' + month + '-' + day
    return currentDate

def upload(clickElement, uploadElement, uploadFile, cofirmElement):
    elementObj = driver.getElementByAttribute(clickElement)
    elementObj.click()
    logger.info('测试【导入】功能')
    logger.info('点击“导入”')
    elementObj= driver.findElement(uploadElement)
    elementObj.send_keys(uploadFile)
    time.sleep(1)
    logger.info('导入“附件”')
    elementObj = driver.getElementByAttribute(cofirmElement)
    elementObj.click()
    logger.info('点击“确定”')
    time.sleep(2)